package cn.chenhuanming.shh.administer.intermediary.business.eventHandler;

import cn.chenhuanming.shh.administer.entity.jpa.UserBroker;
import cn.chenhuanming.shh.administer.entity.jpa.UserSystem;
import cn.chenhuanming.shh.administer.repository.UserSystemRepository;
import cn.chenhuanming.shh.administer.utils.AuthorityTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.stereotype.Component;

/**
 * Created by chenhuanming on 2017-08-23.
 *
 * @author chenhuanming
 */
@Component
@RepositoryEventHandler
public class UserBrokerEventHandler {

    @Autowired
    UserSystemRepository userSystemRepository;

    @HandleBeforeCreate
    public void beforeCreate(UserBroker userBroker) throws IllegalAccessException {
        String role = AuthorityTool.getRole();
        if(role!=null&&role.equals("ROLE_SHOP_MANAGER")){
            UserSystem manager = userSystemRepository.findOne(AuthorityTool.getPrincipal());
            userBroker.setUserSystem(manager);
        }

    }

}
